package com.boang.lock;

import java.util.concurrent.locks.ReentrantLock;

/*
  @author: 13813
  @date: 2024/11/26 13:57
*/
public class TicketDemoLock05 implements Runnable {
    private int num = 20;
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            //获取当前线程
            lock.lock();//上锁
            if (num > 0) {
                try {
                    Thread.sleep(300);//t0
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + ":" + num--); //t0 0 t1 -1 t2 -2
            }
            lock.unlock();//解锁
        }
    }
}
